<template>
  <el-dialog
      title="选择数据"
      :visible.sync="dialogTableVisible"
      :before-close="handleClose">
    <el-table
        ref="singleTable"
        :data="productShows"
        style="width: 100%"
        highlight-current-row
        @current-change="handleCurrentChange"
        border>
      <el-table-column
          prop=""
          label="选择产品"
          width="80">
        <template slot-scope="scope">
          <el-checkbox-group v-model="checkList">
            <el-checkbox :label="scope.row.id">
              <span style="display: none;">{{ scope.row.id }}</span>
            </el-checkbox>
          </el-checkbox-group>
        </template>
      </el-table-column>
      <el-table-column
          property="productName"
          label="产品名称"
          width="150">
      </el-table-column>
      <el-table-column
          property="productCode"
          label="产品编码"
          width="200">
      </el-table-column>
      <el-table-column
          property="productBrand"
          label="品牌">
      </el-table-column>
      <el-table-column
          property="productModels"
          label="规格型号">
      </el-table-column>
      <el-table-column
          property="priductUnit"
          label="单位">
      </el-table-column>
    </el-table>
    <!-- 按钮区域 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="clickButton">确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  name: "ChooseProductView",
  props: {
    open: {
      type: Boolean
    },
  },
  data() {
    return {
      checkList: [],
      currentRow: null,
      productShows: [],//产品
      dialogTableVisible: false,
      selectedProductCodes: [],
    };
  },
  watch: {
    open: function (newVal) {
      this.dialogTableVisible = newVal
    },
    selectedProductCodes: function (newVal) {
      this.currentRow = newVal;
    },
  },
  methods: {
    handleCurrentChange(val) {
      this.currentRow = val;
      this.$emit("checkList", this.checkList)
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            done();
            this.$emit("close")
          })
          .catch(_ => {
          });
    },
    clickButton() {
      this.tableDatas = [];
      for (let item of this.checkList) {
        // 对于 checklist 中的每个元素的 id
        let id = item;
        const selectedProductShow = this.productShows.find(productShow => productShow.id === id);
        this.selectedProductCodes.push(selectedProductShow);
      }
      this.$emit("selectedProductCodes", this.selectedProductCodes)
    },
  },
  created() {
    this.$axios.get("/manage/product/list").then(response => {
      this.productShows = response.data.productShows
    })
  }
};
</script>

<style scoped>

</style>